/*using double liked list to implement double queue*/

var DbNode = require('./node');

class DbQueue {
    constructor() {
        this._first = null;
        this._last = null;
        this._count = 0;
    }

    isEmpty() {
        return this._count === 0;
    }

    size() {
        return this._count;
    }

    pushLeft(item) {
        if (item === 'undefinded') {
            throw Error("No Arguments");
        }

        let node = new DbNode();
        node.value = item;
        if (this._count === 0) {
            this._last = node;
        } else {
            this._first.prev = node;
            node.next = this._first;
        }
        this._first = node;
        this._count++;
    }

    popLeft() {
        if (this._count <= 0) {
            throw Error("No Value");
        }

        let item = this._first.value;
        this._first = this._first.next;
        this._count--;

        if (this._count <= 0) {
            this._last = null;
        } else {
            this._first.prev = null;
        }
        return item;
    }

    pushRight(item) {
        if (item === 'undefinded') {
            throw Error("No Arguments");
        }

        let node = new DbNode();
        node.value = item;
        if (this._count === 0) {
            this._first = node;
        } else {
            this._last.next = node;
            node.prev = this._last;
        }
        this._last = node;
        this._count++;
    }

    popRight() {
        if (this._count <= 0) {
            throw Error("No Value");
        }

        let item = this._last.value;
        this._last = this._last.prev;
        this._count--;

        if (this._count <= 0) {
            this._first = null;
        } else {
            this._last.next = null;
        }
        return item;
    }
}

module.exports = DbQueue;